package com.qf.service.impl;

import com.qf.dao.CommentDao;
import com.qf.dao.MovieDao;
import com.qf.dao.repository.MovieRepository;
import com.qf.pojo.resp.BaseResp;
import com.qf.pojo.vo.Actor;
import com.qf.pojo.vo.Comment;
import com.qf.pojo.vo.Movie;
import com.qf.service.MovieService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;

import static org.apache.ibatis.ognl.DynamicSubscript.all;

@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    MovieDao movieDao;

    @Autowired
    CommentDao commentDao;

    @Autowired
    MovieRepository movieRepository;

    @Autowired
    RedisTemplate redisTemplate;

    protected Logger logger= LoggerFactory.getLogger(RotationServiceImpl.class);

    @Override
    public BaseResp findMovieById(Integer movieId) {
        Object movie_id = redisTemplate.opsForHash().get("Movie_ID", movieId.toString());
        Object baseMoive=null;
        if (movie_id==null){
            baseMoive = movieDao.findMovieById(movieId);
            redisTemplate.opsForHash().put("Movie_ID", movieId.toString(),baseMoive);
            redisTemplate.opsForHash().get("Movie_ID", movieId.toString());
            logger.info("从redie中获取电影详情",baseMoive);
        }else {
            baseMoive= redisTemplate.opsForHash().get("Movie_ID", movieId.toString());
            logger.info("从SQL中获取电影详情",baseMoive);
        }
        return new BaseResp(0,"电影详情查询成功",null,baseMoive);

    }

    @Override
    public BaseResp findByUserComment(Integer id,Integer page,Integer size) {
        Object comment = redisTemplate.opsForHash().get("COMMENT", id.toString());
        Object list=null;
        if (comment==null){
            List<Comment>  byUserComent = commentDao.findByUserComent(id);
           Map map=new HashMap();
           map.put(id.toString(),byUserComent);
           redisTemplate.opsForHash().putAll("COMMENT",map);
           list=redisTemplate.opsForHash().get("COMMENT", id.toString());
            logger.info("从redie中获取电影评论",list);
        }else {
            list=   redisTemplate.opsForHash().get("COMMENT", id.toString());
            logger.info("从Sql中获取电影评论",list);
        }

        return new BaseResp(0,"按电影查询用户评论",null,list);
    }
    @Override
    public BaseResp findAll() {
        Boolean movieall = redisTemplate.hasKey("MOVIEALL");
        Object movieall1 =null;
        if (!movieall){
            List<Movie> all = movieRepository.findAll();
            redisTemplate.opsForValue().set("MOVIEALL",all);
             movieall1 = redisTemplate.opsForValue().get("MOVIEALL");
            logger.info("从redie中获取所有电影",all);
        }else {
            movieall1 = redisTemplate.opsForValue().get("MOVIEALL");
            logger.info("从SQL中获取所有电影",all);
        }
        return new BaseResp(0,"查询成功",null,movieall1);
    }

    @Override
    public BaseResp findMovieByType(Integer typeId) {
        Object movie_type = redisTemplate.opsForHash().get("MOVIE_TYPE", typeId.toString());
        Object movie_type1 =null;
        if (movie_type==null){
            movie_type1 = movieDao.findMovieByType(typeId);
            redisTemplate.opsForHash().put("MOVIE_TYPE", typeId.toString(),movie_type1);
            movie_type1=  redisTemplate.opsForHash().get("MOVIE_TYPE", typeId.toString());
            logger.info("从redie中获取相关电影电影",all);
        }else {
            movie_type1=  redisTemplate.opsForHash().get("MOVIE_TYPE", typeId.toString());
            logger.info("从SQL中获取相关电影电影",all);
        }
        return new BaseResp(0,"按照类型查询电影",null,movie_type1);
    }

}
